package org.inet.minispring;

import org.inet.minispring.bean.beanfactory.BeanFactory;
import org.inet.minispring.bean.beanfactory.SimpleBeanFactory;
import org.inet.minispring.event.ApplicationEvent;
import org.inet.minispring.event.ApplicationEventPublisher;
import org.inet.minispring.exception.BeansException;
import org.inet.minispring.resource.ClassPathXmlResource;
import org.inet.minispring.resource.Resource;


public class ClassPathXmlApplicationContext implements BeanFactory, ApplicationEventPublisher {

    private final SimpleBeanFactory beanFactory = new SimpleBeanFactory();

    public ClassPathXmlApplicationContext(String fileName) {
        Resource resource = new ClassPathXmlResource(fileName);
        var reader = new XmlBeanDefinitionReader(beanFactory, beanFactory);
        reader.loadBeanDefinitions(resource);
    }

    /**
     * 从容器中获取 bean 实例
     * @param beanName
     * @return
     */
    public Object getBean(String beanName) throws BeansException {
        return beanFactory.getBean(beanName);
    }

    @Override
    public boolean containsBean(String beanName) {
        return this.beanFactory.containsBean(beanName);
    }

    @Override
    public boolean isSingleton(String beanName) {
        return false;
    }

    @Override
    public boolean isPrototype(String beanName) {
        return false;
    }

    @Override
    public Class<?> getType(String beanName) {
        return null;
    }

    @Override
    public void publishEvent(ApplicationEvent event) {

    }
}
